//
//  UILabel+computeheight.m
//  ebook
//
//  Created by lj on 14-6-20.
//  Copyright (c) 2014年 cczone. All rights reserved.
//

#import "UILabel+computeheight.h"

static UILabel *instance = 0;

@implementation UILabel (computeheight)

+(UILabel *)getInstance
{
    if(!instance)
    {
        instance = [[UILabel alloc] init];
    }
    return instance;
}

+(CGFloat)CompareTextHeight:(UILabel *)label
{
    return [UILabel CompareTextHeight:label.text Width:label.frame.size.width FontSize:label.font.pointSize Lines:label.numberOfLines];
}

+(CGSize)CompareTextSize:(NSString*)text Size:(CGSize)size FontSize:(CGFloat)fontSize
{
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    CGSize rsize = CGSizeZero;
    if(IOS7)
    {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
        
        rsize = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    }
    else
        rsize = [text sizeWithFont:font constrainedToSize:size];
    return rsize;
}

+(CGFloat)CompareTextHeight:(NSString*)text Width:(CGFloat)width FontSize:(CGFloat)fontSize Lines:(int)lines
{
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    CGSize size = CGSizeZero;
    CGFloat maxHeight = (lines == 0)?MAXFLOAT:((fontSize+2.5)*(lines+1));
    
    if(IOS7)
    {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
        
        size = [text boundingRectWithSize:CGSizeMake(width, maxHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    }
    else
        size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, maxHeight)];
    return size.height;
}

+(CGFloat)CompareTextWidth:(NSString*)text andFontSize:(CGFloat)fontSize
{
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    CGSize size = CGSizeZero;
    
    if(IOS7)
    {
        /*
         This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
         */
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
        
        size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    }
    else
        size = [text sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    return size.width;
}

#pragma mark 获取label大小
+(CGSize)getSizeWithText:(UILabel *)label andLabelWidth:(CGFloat)labelWidth{
    if (label.text == nil) {
        label.text = @"";
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:label.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];
    label.attributedText = attributedString;
    return [label sizeThatFits:CGSizeMake(labelWidth, MAXFLOAT)];
}

/**
 *  计算文本高度
 *
 *  @param text       文本内容
 *  @param number     行数
 *  @param width      文本宽度
 *  @param heightLine 行间距
 *  @param headerLine 首行缩进多少
 *  @param labelInfo  返回处理好的信息
 */
+(void)getSizeWithText:(NSString *)text numberOfLine:(int)number labelWidth:(CGFloat)width heightLine:(CGFloat)heightLine headerLine:(CGFloat)headerLine labelInfo:(labelSizeAndTextBlk)labelInfo{
    
    if (text == nil) {
        text = @"暂无简介";
    }
    
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.numberOfLines = number;
    label.font = [UIFont systemFontOfSize:FONT_LITTLE_MIDDLE];
    
    //UILabel设置行间距等属性：
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:label.text];;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
    [paragraphStyle setLineSpacing:heightLine];//行间距
    [paragraphStyle setFirstLineHeadIndent:headerLine];//首行缩进
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];
    
    label.attributedText = attributedString;

    //宽度不变，根据字的多少计算label的高度
    labelInfo([label sizeThatFits:CGSizeMake(width, MAXFLOAT)],attributedString);
}

@end
